package com.jchy.renderit.core;
 
public class PropertiesAndMethods
{
    public static void prompt(Object anObject)
    {
        System.out.println(anObject.getClass().getName());
        System.out.println(anObject);
        System.out.println("Fields:");
        for(int i=0;i<anObject.getClass().getFields().length;i++)
        {
            if("java.lang.Object".compareTo(anObject.getClass().getFields()[i].getDeclaringClass().getName())==0)
                continue;
            System.out.print("(");
            System.out.print(anObject.getClass().getFields()[i].getDeclaringClass().getName());
            System.out.print(") ");
            System.out.print(anObject.getClass().getFields()[i].getType().getName());
            System.out.print(" ");
            System.out.print(anObject.getClass().getFields()[i].getName());
            if(anObject.getClass().getFields()[i].getType().isPrimitive() &&
               !anObject.getClass().getFields()[i].getType().isArray())
            {
                if("double".compareTo(anObject.getClass().getFields()[i].getType().getName())==0)
                {
                    System.out.print(" ");
                    try
                    {
                        System.out.print(anObject.getClass().getFields()[i].getDouble(anObject));
                    }
                    catch(IllegalArgumentException argex)
                    {
                        System.out.print(argex.getMessage());
                    }
                    catch(IllegalAccessException accex)
                    {
                        System.out.print(accex.getMessage());
                    }
                } // if double
                if("int".compareTo(anObject.getClass().getFields()[i].getType().getName())==0)
                {
                    System.out.print(" ");
                    try
                    {
                        System.out.print(anObject.getClass().getFields()[i].getInt(anObject));
                    }
                    catch(IllegalArgumentException argex)
                    {
                        System.out.print(argex.getMessage());
                    }
                    catch(IllegalAccessException accex)
                    {
                        System.out.print(accex.getMessage());
                    }
                } // if int
                System.out.print(" Change? ");
                String changeit=MyInput.readString();
                if(("Y".compareTo(changeit)==0)||("y".compareTo(changeit)==0))
                {
                    double aDouble;
                    int anInt;
                    System.out.print(anObject.getClass().getFields()[i].getType().getName());
                    System.out.print(": ");
                    if("double".compareTo(anObject.getClass().getFields()[i].getType().getName())==0)
                    {
                        aDouble=MyInput.readDouble();
                        try
                        {
                            anObject.getClass().getFields()[i].setDouble(anObject,aDouble);
                            System.out.print(" ran ok ");
                        }
                        catch(IllegalArgumentException argex)
                        {
                            System.out.print(argex.getMessage());
                        }
                        catch(IllegalAccessException accex)
                        {
                            System.out.print(accex.getMessage());
                        }   
                    }  // double field
                    if("int".compareTo(anObject.getClass().getFields()[i].getType().getName())==0)
                    {
                        anInt=MyInput.readInt();
                        try
                        {
                            anObject.getClass().getFields()[i].setInt(anObject,anInt);
                            System.out.print(" ran ok ");
                        }
                        catch(IllegalArgumentException argex)
                        {
                            System.out.print(argex.getMessage());
                        }
                        catch(IllegalAccessException accex)
                        {
                            System.out.print(accex.getMessage());
                        }   
                    }  // int field
                } // if user chose to change the field
            } // if primitive and not array
            else System.out.println();
        } // for i over public fields of the class
        
        System.out.println("Methods:");
        for(int i=0;i<anObject.getClass().getMethods().length;i++)
        {
            if("java.lang.Object".compareTo(anObject.getClass().getMethods()[i].getDeclaringClass().getName())==0)
                continue;
            System.out.print("(");
            System.out.print(anObject.getClass().getMethods()[i].getDeclaringClass().getName());
            System.out.print(") ");
            System.out.print(anObject.getClass().getMethods()[i].getReturnType().getName());
            System.out.print(" ");
            System.out.print(anObject.getClass().getMethods()[i].getName());
            System.out.print(" (");
            int notPrimitive=0;
            int paramCount=0;
            for(int j=0;j<anObject.getClass().getMethods()[i].getParameterTypes().length;j++)
            {
                paramCount++;
                if(j>0) System.out.print(", ");
                System.out.print(anObject.getClass().getMethods()[i].getParameterTypes()[j].getName());
                if(!anObject.getClass().getMethods()[i].getParameterTypes()[j].isPrimitive())
                   notPrimitive++;
            } // for j over parameters of method[i] of the class
            System.out.print(")");
            if((notPrimitive==0)&&(paramCount>0))
            {
                System.out.print(" Call? ");
                String callit=MyInput.readString();
                if(("Y".compareTo(callit)==0)||("y".compareTo(callit)==0))
                {
                    Object params[]=new Object[anObject.getClass().getMethods()[i].getParameterTypes().length];
                    for(int k=0;k<anObject.getClass().getMethods()[i].getParameterTypes().length;k++)
                    {
                        double aDouble;
                        int anInt;
                        Double aDoubleObject;
                        Integer anIntegerObject;
                        System.out.print(anObject.getClass().getMethods()[i].getParameterTypes()[k].getName());
                        System.out.print(": ");
                        if("double".compareTo(anObject.getClass().getMethods()[i].getParameterTypes()[k].getName())==0)
                        {
                            aDouble=MyInput.readDouble();
                            aDoubleObject=new Double(aDouble);
                            params[k]=aDoubleObject;
                        }
                        if("int".compareTo(anObject.getClass().getMethods()[i].getParameterTypes()[k].getName())==0)
                        {
                            anInt=MyInput.readInt();
                            anIntegerObject=new Integer(anInt);
                            params[k]=anIntegerObject;
                        }
                    } // for k over parameters of method[i] of the class
                    try
                    {
                        Object result=anObject.getClass().getMethods()[i].invoke(anObject,params);
                        if(result==null) System.out.println(" ran ok ");
                        else System.out.println(result);
                    }
                    catch(IllegalAccessException accex)
                    {
                        System.out.print(accex.getMessage());
                    }
                    catch(IllegalArgumentException argex)
                    {
                        System.out.print(argex.getMessage());
                    }
                    catch(java.lang.reflect.InvocationTargetException invtarex)
                    {
                        System.out.print(invtarex.getMessage());
                    }
                    
                } // if user selected to call this method
                
            } // if this method returns void and all parameters are primitive
            else System.out.println();

        } // for i over public methods of the class
        System.out.println(anObject);        
    }
}